function insertarFila(tableID) {
    var campo_cant = document.getElementById('cant');
    var cant = parseInt(campo_cant.value) + 1;
    campo_cant.value = cant;
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    row.id = 'fila_' + cant;
    var cell1 = row.insertCell(0);
    var element1 = document.createElement("input");
    element1.type = "text";
    cell1.appendChild(element1);
    var cell2 = row.insertCell(1);
    var element2 = document.createElement("div");
    element2.innerHTML = '<img width="15" height="15" border="0" align="absmiddle" title="Eliminar" alt="Eliminar" id="eliminar" style="cursor: pointer;" onclick="eliminarFila(' + cant + ');" src="/img/iconos/icons/delete.gif">';
    cell2.appendChild(element2);
}

function eliminarFila(rowIndex) {
    if(confirm('¿Desea eliminar este viaje?')){
        var row = document.getElementById('nuevo' + rowIndex);
        row.parentNode.removeChild(row);
    }
}

function calcular(){
    var peso = (document.getElementById('peso').value != '') ? document.getElementById('peso').value : '0';
    var precio = (document.getElementById('precio_ton').value != '') ? document.getElementById('precio_ton').value : '0';
    var total;
    peso = peso.replace(/\,/g, '.');
    precio = precio.replace(/\,/g, '.');
    total = (parseFloat(peso) * parseFloat(precio)).toFixed(2).toString().replace(/\./g, ',');
    document.getElementById('flete').value = total;
}

function desmar(id){
    var elemento = document.getElementById(id);
    elemento.value = (elemento.checked) ? '1' : '0';
}

function calcularSubTotales(tabla, elemento, iva){
        var rows = document.getElementById(tabla).getElementsByTagName(elemento);
        var t_ton = 0, t_fle = 0, t_cal = 0, t_con = 0, contar = false;
        for(var x = 0; x < (rows.length) - 5; x++) {
            for(var i = 0; i < rows[0].cells.length; i++){
                var clases = rows[x].cells[i].className.split(" ");
                if((rows[x].cells[i].childNodes[0].type == 'checkbox') && rows[x].cells[i].childNodes[0].checked)
                    contar = true;
                
                if(contar){
                    for(var j=0, jl=clases.length; j<jl; j+=1){
                        if(clases[j] == 'ton')
                            t_ton += parseFloat(rows[x].cells[i].firstChild.data.replace(/\./g, '').replace(/\,/g, '.'));
                        else if(clases[j] == 'fle')
                            t_fle += parseFloat(rows[x].cells[i].firstChild.data.replace(/\./g, '').replace(/\,/g, '.'));
                        else if(clases[j] == 'cal')
                            t_cal += parseFloat(rows[x].cells[i].firstChild.data.replace(/\./g, '').replace(/\,/g, '.'));
                        else if(clases[j] == 'con')
                            t_con += parseFloat(rows[x].cells[i].firstChild.data.replace(/\./g, '').replace(/\,/g, '.'));
                    }
                }
            }
            
            contar = false;
        }
        document.getElementById('st_ton').innerHTML = t_ton.toFixed(2).toString().replace(/\./g, ',');
        document.getElementById('st_fle').innerHTML = t_fle.toFixed(2).toString().replace(/\./g, ',');
        document.getElementById('st_cal').innerHTML = t_cal.toFixed(2).toString().replace(/\./g, ',');
        document.getElementById('st_con').innerHTML = t_con.toFixed(2).toString().replace(/\./g, ',');
        calcularIva(iva);
}

function calcularIva(elemento){
    var t_iva;
    var flete = document.getElementById('st_fle');
    if(elemento.checked){
        t_iva = ((elemento.value * flete.textContent.replace(/\,/g, '.')) / 100);
        document.getElementById('t_iva').innerHTML = t_iva.toFixed(2).toString().replace(/\./g, ',');
    } else
        document.getElementById('t_iva').innerHTML = '0,00';
    calcularTotales();
}

function calcularTotales(){
    var subFlete = document.getElementById('st_fle');
    var iva = document.getElementById('t_iva');
    var fle = document.getElementById('st_con');
    var total;
    total = (parseFloat(subFlete.textContent.replace(/\,/g, '.')) + parseFloat(iva.textContent.replace(/\,/g, '.')) + parseFloat(fle.textContent.replace(/\,/g, '.')));
    document.getElementById('t_fle').innerHTML = total.toFixed(2).toString().replace(/\./g, ',');
    document.getElementById('t_cal').innerHTML = document.getElementById('st_cal').textContent;
    calcularPago();
}

function calcularPago(){
    var totalFlete = document.getElementById('t_fle');
    var totalCaleta = document.getElementById('t_cal');
    var pagar;
    pagar = (parseFloat(totalFlete.textContent.replace(/\,/g, '.')) + parseFloat(totalCaleta.textContent.replace(/\,/g, '.')));
    document.getElementById('t_pagar').innerHTML = pagar.toFixed(2).toString().replace(/\./g, ',');
}